/*******************************************************************************
 ** Name: timer.cpp                                                           **
 ** Description: Timer and timing objects.                                    **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at $WIKI/display/GOVT/License-software.              **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $GAME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 **                                                                           **
 ** Copyright (c) 2009-2011  Barry Gian James <gian@gamingods.net>            **
 ** All rights reserved.                                                      **
 ******************************************************************************/
// Created on March 16, 2010, 9:45 PM
// Modified $Date: 2012-07-27 19:06:32 -0600 (Fri, 27 Jul 2012) $ by $Author: unknown $

// Portions Copyright 2012 Open Design Strategies, LLC
// All Rights Reserved.

#define  Timer_cpp_ID    "$Id: timer.cpp 68814 2012-07-28 01:06:32Z unknown $"

//extern "C" {
//#include <unistd.h>	// alarm()
//#include <string.h> // strerror()
//}
//#include <iostream>
//#include "timer.hpp"
//#include "log.hpp"
//
//void DefaultSignalHandler(int signo,siginfo_t * evp,void * ucontext)
//{
//	std::cerr << "Timer ID: " << evp->si_timerid << " PID: " << evp->si_pid << std::endl;
//	std::cerr << "Signal #: " << signo << " UID: " << evp->si_uid << std::endl;
//	ucontext_t * uc = reinterpret_cast<ucontext_t*>(ucontext);
////	std::cerr << "ucontext_t flags: " << std::hex << uc->uc_flags << " Sigmask: " << uc->uc_sigmask << std::endl;
//}	// TODO fix line above
//
////////////////////////////////////////[ TimeManager
//MonthStrings
//TimeManager::monthName[] = {
//	{ "January", "Jan" },
//	{ "February", "Feb" },
//	{ "March", "Mar" },
//	{ "April", "Apr" },
//	{ "May", "May" },
//	{ "June", "Jun" },
//	{ "July", "Jul" },
//	{ "August", "Aug" },
//	{ "September", "Sept" },
//	{ "October", "Oct" },
//	{ "November", "Nov" },
//	{ "December", "Dec" },
//	{ NULL, NULL }
//};
const char * TimeofDayStr[] = {
		"The sun is just rising above the horizon with a faint glow.",
		"The day has begun.",
		"It's now high noon.",
		"The sun is slowly setting on the horizon in splashes of red and yellow.",
		"Nighttime has officially begun.",
		"It's midnight, the witching hour.",
		0
};

//TimeManager::TimeManager(int m, int h, int d, int mo, int y)
//{
//	timeInfo.min = m;
//	timeInfo.hour = h;
//	timeInfo.day = d;
//	timeInfo.month = mo;
//	timeInfo.year = y;
//}
//const char *
//TimeManager::DiscordianDate(const char * fmt, bool kb, bool pb)
//{
//
//	return NULL;
//}
//const char *
//TimeManager::DaySuffix(int day)
//{
//	const char * suffix;
//	if (day > 4 && day < 20) suffix = "th";
//	else if (day % 10 == 1) suffix = "st";
//	else if (day % 10 == 2) suffix = "nd";
//	else if (day % 10 == 3) suffix = "rd";
//	else suffix = "th";
//}
//int
//TimeManager::IncrMin()
//{
//	if (++timeInfo.min ==60)
//	{
//		timeInfo.min == 0;
//		IncrHour();
//	}
//	return timeInfo.min;
//}
//
//int
//TimeManager::IncrHour()
//{
//	if (++timeInfo.hour == 24)
//	{
//		timeInfo.hour = 0;
//		IncrDay();
//	}
//	// TODO fire TimeofHour event for hour event listeners
//	return timeInfo.hour;
//}
//
//int
//TimeManager::IncrDay()
//{
//	if (++timeInfo.day == 31)
//	{
//		timeInfo.day = 1;
//		IncrMonth();
//	}
//	return timeInfo.day;
//}
//
//int
//TimeManager::IncrMonth()
//{
//	if (++timeInfo.month == 13)
//	{
//		timeInfo.month = 1;
//		IncrYear();
//	}
//	// TODO fire event for new month to let items like loans
//	// update their values
//	return timeInfo.month;
//}
//
//int
//TimeManager::IncrYear()
//{
//	++timeInfo.year;
//	// TODO fire newyearevent to let eventlisteners take action
//	return timeInfo.year;
//}
////////////////////////////////////////[ Timer
//int
//Timer::_id = 0;
//
//tm *
//Timer::TimeDelta(bool bLocalTime, time_t t1, time_t t2)
//{
//	time_t delta = t2 - t1;
//	if (bLocalTime) return localtime(&delta);
//	else return gmtime(&delta);
//}
//
//
//int
//Timer::Remaining(long & sec, long & nsec)
//{
//	if (status != TIMER_OK) return TIMER_ERROR;
//	if (timer_gettime(handle,&spec) == -1) {
//		status = errno;
//		char buff[256];
//		strerror_r(errno,buff,256);
//		debug::log->out(LOG_CRIT,"timer_gettime(%d): %s",errno,buff);
//		return TIMER_ERROR;
//	}
//	sec		= spec.it_value.tv_sec;
//	nsec	= spec.it_value.tv_nsec;
//
//	return TIMER_OK;
//}
//
//long
//Timer::Elapsed()
//{
//	return TIMER_ERROR;
//}
//
//int
//Timer::Run(long startSec, long startNSec, long reloadSec, long reloadNSec)
//{
//	if (status != TIMER_OK) return TIMER_ERROR;
//
//	spec.it_value.tv_sec	= startSec;
//	spec.it_value.tv_nsec	= startNSec;
//	spec.it_interval.tv_sec	= reloadSec;
//	spec.it_interval.tv_nsec= reloadNSec;
//
//	if (timer_settime(handle,0,&spec,0) == -1) {
//		status = errno;
//		char buff[256];
//		strerror_r(errno,buff,256);
//		debug::log->out(LOG_CRIT,"timer_settime(%d): %s",errno,buff);
//		return TIMER_ERROR;
//	}
//	return TIMER_OK;
//}
//
//int
//Timer::Run(time_t startTime, long reloadSec, long reloadNSec)
//{
//	if (status != TIMER_OK) return TIMER_ERROR;
//
//	spec.it_value.tv_sec	= startTime;
//	spec.it_value.tv_nsec	= 0;
//	spec.it_interval.tv_sec	= reloadSec;
//	spec.it_interval.tv_nsec= reloadNSec;
//
//	if (timer_settime(handle,TIMER_ABSTIME,&spec,0) == -1) {
//		status = errno;
//		char buff[256];
//		strerror_r(errno,buff,256);
//		debug::log->out(LOG_CRIT,"timer_settime(%d): %s",errno,buff);
//		return TIMER_ERROR;
//	}
//	return TIMER_OK;
//
//}
//
//int
//Timer::Stop()
//{
//	if (status != TIMER_OK) return TIMER_ERROR;
//
//	spec.it_value.tv_sec	= 0;
//	spec.it_value.tv_nsec	= 0;
//	spec.it_interval.tv_sec	= 0;
//	spec.it_interval.tv_nsec= 0;
//
//	if (timer_settime(handle,0,&spec,0) == -1) {
//		status = errno;
//		char buff[256];
//		strerror_r(errno,buff,256);
//		debug::log->out(LOG_CRIT,"timer_settime(%d): %s",errno,buff);
//		return TIMER_ERROR;
//	}
//	return TIMER_OK;
//}
//
//int
//Timer::Overrun()
//{
//	if (status != TIMER_OK) return TIMER_ERROR;
//	return timer_getoverrun(handle);
//}
//
//void
//Timer::Alarm(int sec)
//{
//    alarm(sec);
//}
//
////! signum = signal to raise, action = signal handler
////! timerID = this timer's ID, clock = type of system clock
//Timer::Timer(int signum, sigFunc action, int timerID, clock_t clock)
//{
//	timerID = incr();
//	status = TIMER_OK;
//	struct sigaction sigv;
//
//	sigemptyset(&sigv.sa_mask);
//	sigv.sa_flags = SA_SIGINFO;	// so we can use our sigFunc
//	sigv.sa_sigaction = action;
//
//	if (sigaction(signum,&sigv,0) == -1) {
//		status = errno;
//		char buff[256];
//		strerror_r(errno,buff,256);
//		debug::log->out(LOG_CRIT,"sigaction(%d): %s",errno,buff);
//	} else {
//		sigevent sigx;
//		sigx.sigev_notify	= SIGEV_SIGNAL;
//		sigx.sigev_signo	=  signum;
//		sigx.sigev_value.sival_int = timerID;
//
//		if (timer_create(clock,&sigx,&handle) == -1) {
//			status = errno;
//			char buff[256];
//			strerror_r(errno,buff,256);
//			debug::log->out(LOG_CRIT,"timer_create(%d): %s",errno,buff);
//		}
//	}
//}
//
//Timer::~Timer()
//{
//	if (status == TIMER_OK) {
//		Stop();
//		if (timer_delete(handle) == -1) {
//			char buff[256];
//			strerror_r(errno,buff,256);
//			debug::log->out(LOG_CRIT,"timer_delete(%d): %s",errno,buff);
//		}
//	}
//}
//
//// TODO fix
////std::ostream &
////Timer::operator << (std::ostream & o, const Timer & t)
////{
////	long sec, nsec;
////	t.Remaining(sec,nsec);
////	double tval = sec + ((double)nsec/1000000000.0);
////	o << "Time remaining: " << tval << std::endl;
////	return o;
////}
//
